from redis import *

#string数据类型
def redstring():
    #曾加
    try:
        def redisadd(key, vlue):
            st.set(key, vlue)
            result = st.get(key)
            print(f'以增加成功{result}')
    except Exception as e:
        print("e")

    #删除
    try:

        def redisdel(key):
            result = st.get(key)
            if result:
                print("以删除成功", result)
            else:
                print(f'未找到{key}请重新输入')
            st.delete(key)
    except Exception as e:
        print("e")
    #查询
    try:
        def redisget(key):
            result = st.get(key)
            if result:
                print(f'{key}的值是{result}')
            else:
                print("没有查询到结果")

    except Exception as e:
        print("e")

    if __name__ == '__main__':

        print("1,增加数据")
        print("2,删除数据")
        print("3，查询数据")
        print("4,退出")
        st = StrictRedis('127.0.0.1', 6379, 0)
        while True:
            try:
                print("====================================")
                num = int(input('请输入你需要的功能'))

                if num == 1:
                    k = input("请输入key")
                    v = input("请输入vlue")
                    redisadd(k, v)

                elif num == 2:
                    k = input("请输入key")
                    redisdel(k)

                elif num == 3:
                    k = input('请输入查询的key')
                    redisget(k)
                elif num == 4:
                    print('已退出')
                    break
            except Exception as a:
                print("请输入正确的选项")


#hash数据类型
def redhash():
    try:
        #增加
        def redishset(key, name, vlue):
            st.hset(key, name, vlue)
            result = st.hget(key, name)
            print(f'以增加成功{result}')

        #删除
        def redishdel(key,name):
            if name:
                st.hdel(key, name)
                print(f"已删除{key}的{name}的值")
            else:
                st.delete(key)
                print(f'已删除{key}')

        #查询
        def redishget(key,name):
            result = st.hget(key,name)
            if result:
                print(type(result))
                print(result)
            else:
                print('未查询到该属性值')
    except Exception as hash:
        print(hash)

    if __name__ == '__main__':
        print("1,增加数据")
        print("2,删除数据")
        print("3，查询数据")
        print("4,退出")
        st = StrictRedis('127.0.0.1', 6379, 1)
        while True:
            try:
                print("====================================")
                num = int(input('请输入你需要的功能'))

                if num == 1:
                    k = input('请输入key')
                    n = input('请输入属性name')
                    v = input('请输入vlue')
                    redishset(k, n, v)

                elif num == 2:
                    k = input('请输入key')
                    n = input('请输入属性name')
                    redishdel(k, n)

                elif num == 3:
                    k = input('请输入key')
                    n = input('请输入属性name')
                    redishget(k, n)
                elif num == 4:
                    print('已退出')
                    break
            except Exception as a:
                print("请输入正确的选项")



def redislist():
    # 增
    try:
        def redisadd():
            datalist = input("添加列表名称")
            result = input('请输入值（格式：1,2,3,....）')
            list1 = result.split(sep=',')
            for x in list1:
                st.lpush(datalist, x)
            result = st.lrange(datalist, 0, -1)
            print(result)

        # #删
        def redisslrem():
            datalist = input('请输入删除的列表名称')
            a = input('请输入删除的数值')
            if a in datalist:
                st.lrem(datalist, 0, a)
                print(f"已删除{a}")
            else:
                print('数据不存在')

        # 查
        def redislrange():
            datalist = input('请输入查询的列表名称')
            result = st.lrange(datalist, 0, -1)
            print(result)
    except Exception as c:
        print(c)

    if __name__ == '__main__':

        print("1,增加数据")
        print("2,删除数据")
        print("3，查询数据")
        print("4,退出")
        st = StrictRedis('127.0.0.1', 6379, 2)
        while True:
            try:
                print("====================================")
                num = int(input('请输入你需要的功能'))

                if num == 1:
                    redisadd()
                elif num == 2:
                    redisslrem()

                elif num == 3:
                    redislrange()
                elif num == 4:
                    print('已退出')
                    break
            except Exception as a:
                print("请输入正确的选项")





def redisset1():
    #增
    try:
        def redissadd():
            dataset = input("添加key值")
            result = input('请输入值（格式：1,2,3,....）')
            list1= result.split(sep=',')
            for x in list1:
                st.sadd(dataset,x)
            result =st.smembers(dataset)
            print(result)

        # #删
        def redissrem():
            dataset = input('请输入删除的列表名称')
            a = input('请输入删除的数值')
            result = st.smembers(dataset)
            str1 = str(result)
            if a in str1:
                st.srem(dataset, a)
                print(f"已删除{a}")
            else:
                print('数据不存在')


        #查
        def redissmembers():
            dataset = input('请输入查询的列表名称')
            result = st.smembers(dataset)
            print(result)
    except Exception as d:
        print(d)


    if __name__ == '__main__':

        print("1,增加数据")
        print("2,删除数据")
        print("3，查询数据")
        print("4,退出")
        st = StrictRedis('127.0.0.1', 6379, 3)
        while True:
            try:
                print("====================================")
                num = int(input('请输入你需要的功能'))

                if num == 1:
                    redissadd()
                elif num == 2:
                    redissrem()

                elif num == 3:
                    redissmembers()
                elif num == 4:
                    print('已退出')
                    break
            except Exception as a:
                print("请输入正确的选项")
set()

def rediszset1():
    #增
    try:
        def rediszadd():
            datazset = input("添加key值")
            result = input('请输入值（格式：score1,nubmer1,）')
            list = result.split(sep=',')
            a, b = list
            mapping = {
                a: int(b)
            }
            st.zadd(datazset, mapping)
            result = st.zrange(datazset, 0, -1)
            print(str(result))

        # #删
        def rediszrem():
            datazet = input('请输入删除的key值')
            a = input('请输入删除的number')
            result = st.zrange(datazet, 0, -1)
            if a in str(result):
                st.zrem(datazet, a)
                print('已删除')
            else:
                print("数据不存在")

        #查
        def redissrange():
            datazet = input('请输入查询的列表名称')
            result = st.zrange(datazet, 0, -1)
            print(result)
    except Exception as e:
        print(e)



    if __name__ == '__main__':

        print("1,增加数据")
        print("2,删除数据")
        print("3,查询数据")
        print("4,退出")
        st = StrictRedis('127.0.0.1', 6379, 4)
        while True:
            try:
                print("====================================")
                num = int(input('请输入你需要的功能'))

                if num == 1:
                    rediszadd()
                elif num == 2:
                    rediszrem()

                elif num == 3:
                    redissrange()
                elif num == 4:
                    print('已退出')
                    break
            except Exception as a:
                print("请输入正确的选项")



if __name__ == '__main__':
    print("1,string操作")
    print("2,hash操作")
    print("3,list操作")
    print("4,set操作")
    print("5,zset操作")
    a = int(input("请输入操作数据类型"))
    if a == 1:
        redstring()
    elif a==2:
        redhash()
    elif a ==3:
        redislist()
    elif a==4:
        redisset1()
    elif a==5:
        rediszset1()
    else:
        print('请输入正确的选项')

